/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var contestapp = {    
    isConnectedFacebook: false,
    callbackscript: "",
    isAcceptUnlike: false,
    isEndOfContest: false,
    isDenyVote: false,
    images:{
        unlike: ""
    },
    alertbox: null,
    likecomment: function(dataID){
        if(!contestapp.isDenyVote) {
            if(contestapp.isConnectedFacebook) {
                contestapp.blockUIMessageBox("We record your voice, please wait");
                jQuery.post("?action=like", {data_id: dataID}, function(data) {
                    var r = eval("(" + data + ")");
                    if(r.status == "1"){

                        var voteText = "vote";
                        if(r.numberoflike != "1") {
                            voteText = "votes";                        
                        }

                        jQuery("td#like-area-" + r.data_id).html('<a href="javascript:contestapp.unlikecomment(' + r.data_id + ');"></a>');
                        jQuery("div#like-area-" + r.data_id).html('<a href="javascript:contestapp.unlikecomment(' + r.data_id + ');"><img src="' + contestapp.images.unlike + '" /></a>');
                        jQuery("span#number-of-like-" + r.data_id).html(r.numberoflike + " " + voteText);                     
                        jQuery("div#home-total-like-" + r.data_id).html(r.numberoflike);
                    } else {
                        contestapp.callbackscript = "contestapp.likecomment(" + dataID + ")";
                        contestapp.loginFacebook();
                    }    
                    jQuery.unblockUI();
                });
            } else {
                contestapp.callbackscript = "contestapp.likecomment(" + dataID + ")";
                contestapp.loginFacebook();
            }
        } else {
            if(contestapp.alertbox){
                contestapp.alertbox.open();
            } else {
                contestapp.alertbox = new LightFace({
                    title: 'Information',
                    content: 'Voting will be opened from 20.01.2013',
                    buttons: [
                    {
                            title: 'Close', 
                            event: function() {
                                this.close();
                            }, 
                            color: 'blue'
                        }
                    ]
                });
                contestapp.alertbox.open();
            }
        }                
    },
    tosbox: null,
    tostitle: "TOS",
    showTOSWindow: function() {            
        var randString = Math.random();
        var objectURL = '?action=tos&rnd=' + randString;
        if(contestapp.tosbox == null) {
            contestapp.tosbox = new LightFace.Request({
                url: objectURL,
                width: 420, 
                draggable: false,
                resetOnScroll: facebooktab.lightboxAutoAdjust,
                zIndex: 999,
                onOpen:function(){
                    if(!facebooktab.lightboxAutoAdjust){
                        facebooktab.resizeLightbox();
                    }                  
                },
                onComplete:function(){
                    if(!facebooktab.lightboxAutoAdjust){
                        facebooktab.resizeLightbox();
                    } else {
//                        window.setTimeout("facebooktab.likelightbox._position()", 1000);                        
                    }   
                },
                buttons: [
                    {
                        title: 'Close', 
                        event: function() {
                            this.close();
                        }, 
                        color: 'blue'
                    }
                ],
                request:{
                    evalScripts: true
                },
                title: contestapp.tostitle
            }).open();
        } else {
            contestapp.tosbox.open();                        
        }
        return void(0);
    },
    unlikecomment: function(dataID){    
        if(contestapp.isAcceptUnlike) {
            if(contestapp.isConnectedFacebook) {
                contestapp.blockUIMessageBox("We will delete your voice");
                jQuery.post("?action=unlike", {data_id: dataID}, function(data) {
                    var r = eval("(" + data + ")");
                    if(r.status == "1"){
                        var voteText = "stemme";
                        if(r.numberoflike != "1") {
                            voteText = "stemmer";                        
                        }

                        jQuery("td#like-area-" + r.data_id).html('<a href="javascript:contestapp.likecomment(' + r.data_id + ');"><img src="images/vote_btn.png" alt="" /></a>');
                        jQuery("div#like-area-" + r.data_id).html('<a href="javascript:contestapp.unlikecomment(' + r.data_id + ');"></a>');
                        jQuery("span#number-of-like-" + r.data_id).html(r.numberoflike + " " + voteText);                    
                        jQuery("div#home-total-like-" + r.data_id).html(r.numberoflike);
                    } else {         
                        contestapp.callbackscript = "contestapp.unlikecomment(" + dataID + ")";
                        contestapp.loginFacebook();
                    }
                    jQuery.unblockUI();
                });
            } else {
                contestapp.callbackscript = "contestapp.unlikecomment(" + dataID + ")";
                contestapp.loginFacebook();
            }
        }        
    },
    updatelike: function(dataID) {
        jQuery.post("?action=getnumberoflike", {data_id: dataID}, function(data) {
            var r = eval("(" + data + ")");
            if(r.status == "1"){                
                jQuery("#number-of-like-" + r.data_id).html(r.numberoflike);                      
                jQuery("#home-total-like-" + r.data_id).html(r.numberoflike);
            }
        });
    },
    loginFacebook:function(){
        /*
        FB.login(function(response) {
            if (response.authResponse) {
                console.log('Welcome!  Fetching your information.... ');
                FB.api('/me', function(response) {
                    console.log('Good to see you, ' + response.name + '.');
                    FB.logout(function(response) {
                        console.log('Logged out.');
                    });
                });
            } else {
                console.log('User cancelled login or did not fully authorize.');
            }
        }, {scope: 'email'});
        */
        contestapp.blockUIMessageBox("Connect to Facebook");
        FB.login(function(response) {                
            if (response.authResponse) {                
                var accessToken = response.authResponse.accessToken;
                var userID = response.authResponse.userID;
                jQuery.post("?action=loginfacebook", {
                    accessToken: accessToken, 
                    userID: userID
                }, function(data){  
                    jQuery.unblockUI();
                    var r = eval("(" + data + ")");
                    if(r.status == "1") {
                        contestapp.isConnectedFacebook = true;
                        if(contestapp.callbackscript != "") {
                            eval(contestapp.callbackscript);
                            contestapp.callbackscript = "";
                        } else {
                            window.location.reload(true);
                        }                        
                    }
                });
            } else {                
                contestapp.blockUIMessageBox("Sorry, Could not connect to Facebook");
                
                setTimeout(jQuery.unblockUI, 2000); 
                
                return false;
            }
        }, {scope: 'email,user_about_me,user_birthday,publish_stream,offline_access'});
        
    },
    blockUIMessageBox: function(message) {
        jQuery.blockUI({message: message, 
            css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#000', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: .5, 
            color: '#fff' 
        }}); 
        jQuery('.blockOverlay').attr('title','Click to unblock').click(jQuery.unblockUI); 
    }
};

